package com.zhoug.zmui.alpha;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;

import com.zhoug.common.utils.LogUtils;
import com.zhoug.common.utils.ResourceUtils;
import com.zhoug.zmui.R;

import java.lang.ref.WeakReference;

import androidx.annotation.AttrRes;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import androidx.core.content.res.TypedArrayUtils;

/**
 * 按下或者disabled是改变View的alpha工具
 */
public class AlphaViewHelper {
    private static final String TAG = ">>>AlphaViewHelper";
    private static final boolean DEBUG = false;
    private WeakReference<View> mTarget;

    /**
     * 设置是否要在 press 时改变透明度
     */
    private boolean mChangeAlphaWhenPress = true;

    /**
     * 设置是否要在 disabled 时改变透明度
     */
    private boolean mChangeAlphaWhenDisable = true;

    private float mNormalAlpha = 1f;
    private float mPressedAlpha = 0.5f;
    private float mDisabledAlpha = 0.5f;

    public AlphaViewHelper(@NonNull View target) {
        mTarget = new WeakReference<>(target);
        //主题theme中定义的全局默认值alpha
        mPressedAlpha = ResourceUtils.getAttrFloatValue(target.getContext(), R.attr.zmui_alphaPressed);
        mDisabledAlpha = ResourceUtils.getAttrFloatValue(target.getContext(), R.attr.zmui_alphaDisabled);
        if (mPressedAlpha <= 0) {
            mPressedAlpha = 0.5f;
        }
        if (mDisabledAlpha <= 0) {
            mDisabledAlpha = 0.5f;
        }
        if (DEBUG) {
            LogUtils.d(TAG, "AlphaViewHelper:mPressedAlpha=" + mPressedAlpha);
            LogUtils.d(TAG, "AlphaViewHelper:mDisabledAlpha=" + mDisabledAlpha);
        }
    }

    /**
     * 解析xml中属性并设置到AlphaViewHelper中
     *
     * @param alphaViewHelper AlphaViewHelper
     * @param context         Context
     * @param attrs           AttributeSet
     * @param defStyleAttr    defStyleAttr
     */
    public static void parseTypedArray(AlphaViewHelper alphaViewHelper, Context context, AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
      /*  int[] ins = new int[]{
                R.attr.zmui_alphaDisabled, R.attr.zmui_alphaPressed,
                R.attr.zmui_changeAlphaWhenPress,
                R.attr.zmui_changeAlphaWhenDisable,
        };

        TypedArray a = context.obtainStyledAttributes(attrs, ins, 0, defStyleAttr);
        float disabledAlpha = a.getFloat(0, 0);
        float pressedAlpha = a.getFloat(1, 0);
        boolean changeAlphaWhenPress = a.getBoolean(2, true);
        boolean changeAlphaWhenDisable = a.getBoolean(3, true);
        a.recycle();*/

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IAlphaView, defStyleAttr, defStyleRes);
        float disabledAlpha = a.getFloat( R.styleable.IAlphaView_zmui_alphaDisabled, 0);
        float pressedAlpha = a.getFloat( R.styleable.IAlphaView_zmui_alphaPressed, 0);
        boolean changeAlphaWhenPress = a.getBoolean( R.styleable.IAlphaView_zmui_changeAlphaWhenPress, true);
        boolean changeAlphaWhenDisable = a.getBoolean( R.styleable.IAlphaView_zmui_changeAlphaWhenDisable, true);
        a.recycle();

        if (DEBUG) {
            LogUtils.d(TAG, "parseTypedArray:disabledAlpha=" + disabledAlpha);
            LogUtils.d(TAG, "parseTypedArray:pressedAlpha=" + pressedAlpha);
            LogUtils.d(TAG, "parseTypedArray:changeAlphaWhenPress=" + changeAlphaWhenPress);
            LogUtils.d(TAG, "parseTypedArray:changeAlphaWhenDisable=" + changeAlphaWhenDisable);
        }
        if (disabledAlpha != 0) {
            alphaViewHelper.setDisabledAlpha(disabledAlpha);
        }
        if (pressedAlpha != 0) {
            alphaViewHelper.setPressedAlpha(pressedAlpha);
        }
        alphaViewHelper.setChangeAlphaWhenPress(changeAlphaWhenPress);
        alphaViewHelper.setChangeAlphaWhenDisable(changeAlphaWhenDisable);

    }


    public void setPressedAlpha(float pressedAlpha) {
        this.mPressedAlpha = pressedAlpha;
    }

    public void setDisabledAlpha(float disabledAlpha) {
        this.mDisabledAlpha = disabledAlpha;
    }

    /**
     * 在 {@link View#setPressed(boolean)} 中调用，通知 helper 更新
     *
     * @param current the view to be handled, maybe not equal to target view
     * @param pressed {@link View#setPressed(boolean)} 中接收到的参数
     */
    public void onPressedChanged(View current, boolean pressed) {
        View target = mTarget.get();
        if (target == null) {
            return;
        }
        if (current.isEnabled()) {
            target.setAlpha(mChangeAlphaWhenPress && pressed && current.isClickable() ? mPressedAlpha : mNormalAlpha);
        } else {
            if (mChangeAlphaWhenDisable) {
                target.setAlpha(mDisabledAlpha);
            }
        }
    }

    /**
     * 在 {@link View#setEnabled(boolean)} 中调用，通知 helper 更新
     *
     * @param current the view to be handled, maybe not  equal to target view
     * @param enabled {@link View#setEnabled(boolean)} 中接收到的参数
     */
    public void onEnabledChanged(View current, boolean enabled) {
        View target = mTarget.get();
        if (target == null) {
            return;
        }
        float alphaForIsEnable;
        if (mChangeAlphaWhenDisable) {
            alphaForIsEnable = enabled ? mNormalAlpha : mDisabledAlpha;
        } else {
            alphaForIsEnable = mNormalAlpha;
        }
        if (current != target && target.isEnabled() != enabled) {
            target.setEnabled(enabled);
        }
        target.setAlpha(alphaForIsEnable);
    }

    /**
     * 设置是否要在 press 时改变透明度
     *
     * @param changeAlphaWhenPress 是否要在 press 时改变透明度
     */
    public void setChangeAlphaWhenPress(boolean changeAlphaWhenPress) {
        mChangeAlphaWhenPress = changeAlphaWhenPress;
    }

    /**
     * 设置是否要在 disabled 时改变透明度
     *
     * @param changeAlphaWhenDisable 是否要在 disabled 时改变透明度
     */
    public void setChangeAlphaWhenDisable(boolean changeAlphaWhenDisable) {
        mChangeAlphaWhenDisable = changeAlphaWhenDisable;
        View target = mTarget.get();
        if (target != null) {
            onEnabledChanged(target, target.isEnabled());
        }

    }

}
